package main

import (
	"bytes"
	"errors"
	"fmt"
	"image"
	_ "image/gif"
	_ "image/jpeg"
	"image/png"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"os"
)

func main() {
	fmt.Println("starting")
	resp, err := http.Get("http://7xohdn.com2.z0.glb.qiniucdn.com/word_tribe/test/img/0056f07e79a6739f7e3c58c844b81332A.png")
	fmt.Println(err)
	defer resp.Body.Close()
	img, format, err := image.Decode(resp.Body)
	// fmt.Println("img", fmt.Sprintf("%T", img), err, resp.Body)
	b := img.Bounds()
	imgWidth := b.Max.X
	imgHeight := b.Max.Y
	fmt.Println("w-h:", imgHeight, imgWidth)
	if format == "jpeg" {
		fmt.Println("jpeg")
	} else if format == "png" {
		fmt.Println("png")
		buf := new(bytes.Buffer)
		err := png.Encode(buf, img)
		send_s3 := buf.Bytes()
		fmt.Println(len(send_s3), err, imgWidth*imgHeight)
		buf_end := bytes.NewBuffer(send_s3)
		img_end, format_end, err := image.Decode(buf_end)
		fmt.Println("img", fmt.Sprintf("%T", img_end), err, format_end)
	}

	// img.Rect()
	// dst := image.NewRGBA(image.Rect(0, 0, 80, 80))
	// rgbImg := m.(*image.YCbCr)
	// subImg := rgbImg.SubImage(image.Rect(0, 0, 200, 200)).(*image.YCbCr)
	tmp, err := ImageCopy(img, 0, 0, 800, 200)
	fmt.Println("tmp", err)
	// SaveImage("tmp.png", tmp)
	f, _ := os.Create("tmp1.png") //创建文件
	defer f.Close()               //关闭文件
	png.Encode(f, tmp)

	//
	url := "http://localhost:5000/test"

	bodyBuf := &bytes.Buffer{}
	bodyWriter := multipart.NewWriter(bodyBuf)
	buf := new(bytes.Buffer)
	err = png.Encode(buf, img)
	fmt.Println("err:", err)
	//关键的一步操作
	// bodyWriter.CreateFormFile(fieldname, filename)
	fileWriter, err := bodyWriter.CreateFormFile("png", "upload.png")
	if err != nil {
		fmt.Println("error writing to buffer")
		return
	}

	// //打开文件句柄操作
	// fh, err := os.Open(filename)
	// if err != nil {
	// 	fmt.Println("error opening file")
	// 	return err
	// }
	// defer fh.Close()

	//iocopy
	_, err = io.Copy(fileWriter, buf)
	if err != nil {
		fmt.Println(err)
		return
	}

	contentType := bodyWriter.FormDataContentType()
	fmt.Println("contentType", contentType)
	bodyWriter.Close()

	resp, err = http.Post(url, contentType, bodyBuf)
	fmt.Println(bodyBuf)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()
	resp_body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	fmt.Println("aaa", resp.Status)
	fmt.Println(string(resp_body))
	return
}

func ImageCopy(src image.Image, x, y, w, h int) (image.Image, error) {

	var subImg image.Image

	if rgbImg, ok := src.(*image.YCbCr); ok {
		fmt.Println("image.YCbCr")
		subImg = rgbImg.SubImage(image.Rect(x, y, x+w, y+h)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1
	} else if rgbImg, ok := src.(*image.RGBA); ok {
		fmt.Println("image.RGBA")
		subImg = rgbImg.SubImage(image.Rect(x, y, x+w, y+h)).(*image.RGBA) //图片裁剪x0 y0 x1 y1
	} else if rgbImg, ok := src.(*image.NRGBA); ok {
		fmt.Println("image.NRGBA")
		subImg = rgbImg.SubImage(image.Rect(x, y, x+w, y+h)).(*image.NRGBA) //图片裁剪x0 y0 x1 y1
	} else {
		return subImg, errors.New("图片解码失败")
	}

	return subImg, nil
}
